<?php
/**
 * Created by PhpStorm.
 * User: wangp
 * Date: 2016/6/18
 * Time: 21:21
 */

include_once __DIR__ . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;

$options = [
    'debug' => true,
    'app_id' => 'wx73ce0c7fa5af1828',    //app_id
    'secret' => '4387c33d9a29182e713286c8f48fcf89',    //app_secret
    'token' => "",  //token
    'log' => [
        'level' => 'debug',
        'file'  => 'abc.log',
    ],
    'guzzle' => [
        'timeout' => 3.0,// 超时
        'verify' => false,
    ],
    'oauth' => [
        'scopes' => ['snsapi_base'],
        'callback' => 'wx/oauth_callback.php',
    ],
    'payment' => [
        'merchant_id'        => '1324954301',   //商户ID
        'key'                => 'asdzxcfvgbasdzxcfvgbasdzxcfvgbqw', //商户key
        'cert_path'          => __DIR__ . '/../cert1/apiclient_cert.pem', // 证书
        'key_path'           => __DIR__ . '/../cert1/apiclient_key.pem',      // 证书
        'notify_url'         => 'http://wangpwx.vip.natapp.cn/payback.php',       // 回调地址
    ]
];

$app = new Application($options);

$payment = $app->payment;
$response = $payment->payment->handleNotify(function($notify, $successful) use ($db){
    // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
    $order_id = $notify->out_trade_no;
    $sql = "SELECT * FROM `order` WHERE order_id = '{$order_id}'";
    $order = $db->get_row($sql);
    if (!$order) { // 如果订单不存在
        return 'Order not exist.'; // 告诉微信，我已经处理完了，订单没找到，别再通知我了
    }
    // 如果订单存在
    // 检查订单是否已经更新过支付状态
    if ( $order->status != '0') {
        return true; // 已经支付成功了就不再更新了
    }
    //修改支付状态
    //do something
    return true;
});
$response->send();